package com.hkts.management.loadTime.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.utils.ResultUtil;
import com.hkts.management.loadTime.entity.HktsLoadingTime;
import com.hkts.management.loadTime.mapper.HktsLoadingTimeMapper;
import com.hkts.management.loadTime.service.IHktsLoadingTimeService;
import com.hkts.management.utils.DateConverUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-07-06
 */
@Service
@Transactional
@Slf4j
public class HktsLoadingTimeServiceImpl extends ServiceImpl<HktsLoadingTimeMapper, HktsLoadingTime> implements IHktsLoadingTimeService {


    @Autowired
    private HktsLoadingTimeMapper hktsLoadingTimeMapper;

    @Override
    public IPage<HktsLoadingTime> selectPage(Page<HktsLoadingTime> page, HktsLoadingTime hktsLoadingTime) {
        QueryWrapper<HktsLoadingTime> query = Wrappers.query();
        QueryWrapper<HktsLoadingTime> entityWrapper = getEntityWrapper(query, hktsLoadingTime);
        return hktsLoadingTimeMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<HktsLoadingTime> selectList(HktsLoadingTime hktsLoadingTime) {
        QueryWrapper<HktsLoadingTime> query = Wrappers.query();
        QueryWrapper<HktsLoadingTime> entityWrapper = getEntityWrapper(query, hktsLoadingTime);
        return hktsLoadingTimeMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HktsLoadingTime> getEntityWrapper(QueryWrapper<HktsLoadingTime> entityWrapper, HktsLoadingTime hktsLoadingTime) {
        //条件拼接

        return entityWrapper;
    }
    public Result<Object> addLoadTime(String cargoId,List<String> loadTime){
        switch (loadTime.size()){
            case 0:
                return ResultUtil.error("装货时间不能为空");
            case 1:
                return saveOrUpdate(cargoId,loadTime.get(0));
            case 2:
                return saveOrUpdatebatch(cargoId,loadTime);
            default:
                return ResultUtil.error("装货时间格式不正确");
        }
    }
    public Result<Object> saveOrUpdate(String cargoId,String loadTime){
        String[] loadTimes=loadTime.split(",");
        if(loadTimes.length==0){
            return ResultUtil.error("装货时间不能为空");
        }
        HktsLoadingTime time=new HktsLoadingTime();
        time.setCargoId(cargoId);
        time.setLoadingStartTime(DateConverUtils.parseStringToDateTime(loadTimes[0]));
        if(loadTimes.length>1){
            time.setLoadingEndTime(DateConverUtils.parseStringToDateTime(loadTimes[1]));
        }else {
            time.setLoadingEndTime(null);
        }

        List<HktsLoadingTime> hktsLoadingTimes=getLoadTimeByCargoId(cargoId);
        if(hktsLoadingTimes.size()>0){
            if(hktsLoadingTimes.size()==1){
                time.setId(hktsLoadingTimes.get(0).getId());
                hktsLoadingTimeMapper.updateById(time);
                return ResultUtil.success();
            }else{
                deleteLoadTimeByCargoId(cargoId);
            }
        }
        log.info("时间参数{},时间数据{}",loadTime,time);
        hktsLoadingTimeMapper.insert(time);
        return ResultUtil.success();
    }
    public Result<Object> saveOrUpdatebatch(String cargoId,List<String> loadTime){
        QueryWrapper<HktsLoadingTime> query = Wrappers.query();
        query.lambda().eq(HktsLoadingTime::getCargoId,cargoId);
        Integer count=hktsLoadingTimeMapper.selectCount(query);
        if(count>0){
            deleteLoadTimeByCargoId(cargoId);
        }
        for (String item:loadTime
             ) {
            String[] loadTimes=item.split(",");
            HktsLoadingTime time=new HktsLoadingTime();
            time.setCargoId(cargoId);
            time.setLoadingStartTime(DateConverUtils.parseStringToDateTime(loadTimes[0]));
            if(loadTimes.length>1){
                time.setLoadingEndTime(DateConverUtils.parseStringToDateTime(loadTimes[1]));
            }
            hktsLoadingTimeMapper.insert(time);
        }
        return ResultUtil.success();
    }
    public List<HktsLoadingTime> getLoadTimeByCargoId(String cargoId){
        QueryWrapper<HktsLoadingTime> query = Wrappers.query();
        query.lambda().eq(HktsLoadingTime::getCargoId,cargoId);
        query.orderByAsc("loading_start_time");
       return hktsLoadingTimeMapper.selectList(query);
    }
    public HktsLoadingTime getFirsetLoadTimeByCargoId(String cargoId){
        QueryWrapper<HktsLoadingTime> query = Wrappers.query();
        query.lambda().eq(HktsLoadingTime::getCargoId,cargoId);
       return hktsLoadingTimeMapper.selectList(query).get(0);
    }
    public void deleteLoadTimeByCargoId(String cargoId){
        QueryWrapper<HktsLoadingTime> query = Wrappers.query();
        query.lambda().eq(HktsLoadingTime::getCargoId,cargoId);
         hktsLoadingTimeMapper.delete(query);
    }
}
